package MTT::Controller::Access;
use Moose;
use namespace::autoclean;

BEGIN {extends 'Catalyst::Controller'; }

=head1 NAME

MTT::Controller::Access - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut

use Data::Dumper;
use MTT::Form::Access;

sub set_permission :Private : Args(1) {
    my ( $self, $c, $id_user ) = @_;

	$c->stash->{id_user} = $id_user;

	my $form = MTT::Form::Access->new(
		schema => $c->model('DBIC')->schema,
		ctx	=> $c
	);

	$c->stash(
		template => 'access.tt2',
		form 	 => $form
	);

	if ( $c->req->method eq 'POST' ) {
		
		my $params = $c->req->params->{unpermitted};
		$params = [ $params ] unless ref $params;
		foreach my $id ( @$params ) {
			my $object = $c->model('DBIC::AccessObject')->find({id => $id});
			$object->delete() if $object;
		}

		$params = $c->req->params->{permitted};
		$params = [ $params ] unless ref $params;
		foreach my $id ( @$params ) {
			my $object = $c->model('DBIC::AccessObject')->find({id => $id});
			unless ($object) {
				my $obj = $c->model('DBIC::AccessObject')->create({
						id	=> $id,
						object_type => $c->req->param('object_type'),
						id_user		=> $id_user
					});
			}
		}

		$c->stash->{form} = MTT::Form::Access->new(
			schema => $c->model('DBIC')->schema,
			ctx	=> $c
		);
	}
}

sub get_objects : Path('/access/get_objects') {
	my ($self, $c) = @_;

	$c->stash->{json_data} = {
		zzz => '13',
		xxx => 23
	};
	
	$c->forward('View::JSON');
}

=head1 AUTHOR

A clever guy

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

1;
